package com.shopping.mall.core.module.freemarkerMail.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.freemarkerMail.entity.FreemarkerMail;
import com.shopping.mall.core.module.freemarkerMail.mapper.FreemarkerMailMapper;
import com.shopping.mall.core.module.freemarkerMail.service.FreemarkerMailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "FreemarkerMail")
@RestController
@RequestMapping("/freemarkerMail")
public class FreemarkerMailController {
    @Autowired
    private FreemarkerMailService freemarkerMailService;
    @Autowired
    private FreemarkerMailMapper freemarkerMailMapper;

    @ApiOperation("添加模板")
    @PostMapping("/add")
    public Result<Object> add(@RequestBody FreemarkerMail freemarkerMail){
        return freemarkerMailService.add(freemarkerMail.getTemplate(),freemarkerMail.getMailType());
    }

    @ApiOperation("返回最新添加的模板(全部)")
    @GetMapping("/last")
    public Result<Object> select(){
        return freemarkerMailService.select();
    }

    @ApiOperation("根据mailType返回最新添加的模板")
    @GetMapping("/lastOne")
    public Result<Object> select(@RequestParam Integer mailType){
        return Result.OK(freemarkerMailMapper.getLastTemplate(mailType));
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<FreemarkerMail>> getFreemarkerMailPage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        FreemarkerMail freemarkerMail) {
        IPage<FreemarkerMail> freemarkerMailPage=freemarkerMailService.getFreemarkerMailPage(current,size,freemarkerMail);
        return Result.OK(freemarkerMailPage);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result<FreemarkerMail> update(@Validated(FreemarkerMail.update.class) @RequestBody FreemarkerMail freemarkerMail) {
        freemarkerMailService.updateById(freemarkerMail);
        return Result.OK();
    }

    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<FreemarkerMail> getById(@RequestParam String id){
        FreemarkerMail freemarkerMail=freemarkerMailService.getById(id);
        return Result.OK(freemarkerMail);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<FreemarkerMail>> getAll(FreemarkerMail freemarkerMail){
        List<FreemarkerMail> freemarkerMailList=freemarkerMailService.getAll(freemarkerMail);
        return Result.OK(freemarkerMailList);
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return freemarkerMailService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return freemarkerMailService.batchDelete(ids);
    }
}
